Baeza-Yates-Gonnet algorithm